package GUI.ventas;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class TicketDeCompra extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Compra compra;
	private JPanel central;
	private JTextArea txtDelTicket;
 
	private String cambio;
	private String importePagado;
	private PagoTarjeta usuarioTarjeta;
	private String aux;

	public TicketDeCompra(Compra compra) {
		super();
		this.compra = compra;
		this.setTitle("TICKET");
		this.setBounds(500, 100, 400, 500);
		this.setVisible(true);
		this.setResizable(false);

		Calendar calendario = new GregorianCalendar();
		int hora, minutos, segundos;
		hora = calendario.get(Calendar.HOUR_OF_DAY);
		minutos = calendario.get(Calendar.MINUTE);
		segundos = calendario.get(Calendar.SECOND);
		/**
		 * fecha
		 */
		Date now = new Date();
		DateFormat df = DateFormat.getDateInstance();
		String s = df.format(now);
		/**
		 * 
		 */
		
				
		this.setLayout(new BorderLayout(0, 0));

		central = new JPanel();
		central.setBorder(new TitledBorder(new EtchedBorder(), "TICKET DE COMPRA"));
		this.add(central, BorderLayout.CENTER);
		txtDelTicket = new JTextArea("", 30, 32);
		txtDelTicket.setEditable(false);
		JScrollPane scroll = new JScrollPane(txtDelTicket);
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		central.add(scroll, BorderLayout.NORTH);
		this.pack();
		txtDelTicket.setText("*************************TICKET G-MARKET***********************"+"\n"+"\n");
		txtDelTicket.append("Fecha: " + s + "\t" +"\t" +"Hora: " + hora+ ":" + minutos + ":" + segundos+"\n" );
		//txtDelTicket.append("--------------------------------------------------------------------------------------"+"\n");
		txtDelTicket.append("\n"+"\n"+"\n");
		this.listaProductos();
		String aux = Integer.toString(compra.gettotalFinal());
		txtDelTicket.append("--------------------------------------------------------------------------------------");
		txtDelTicket.append("\n"+ "\t" +"\t" +"\t" +"TOTAL = "+aux+" �"+"\n");		
		txtDelTicket.append("N�MERO DE FACTURA: "+"\t"+compra.getnclienteString()+"\n");
		txtDelTicket.append("N�MERO DE VENDEDOR: "+"\t"+compra.getnVendedor()+"\n");
		txtDelTicket.append("--------------------------------------------------------------------------------------"+"\n");
		txtDelTicket.append("Guarde este ticket para posibles devoluciones"+"\n");
		txtDelTicket.append("Presentando tu ticket en un m�x. de 30 d�as"+"\n");


	}
	
	
	
	/**
	 * 
	 */
	
	public TicketDeCompra(Compra t,String usuario) {
		super();
		this.compra = t;
		this.aux = usuario;
		this.setTitle("TICKET");
		this.setBounds(500, 100, 400, 500);
		this.setVisible(true);
		this.setResizable(false);

		Calendar calendario = new GregorianCalendar();
		int hora, minutos, segundos;
		hora = calendario.get(Calendar.HOUR_OF_DAY);
		minutos = calendario.get(Calendar.MINUTE);
		segundos = calendario.get(Calendar.SECOND);
		/**
		 * fecha
		 */
		Date now = new Date();
		DateFormat df = DateFormat.getDateInstance();
		String s = df.format(now);
		/**
		 * 
		 */
		
				
		this.setLayout(new BorderLayout(0, 0));

		central = new JPanel();
		central.setBorder(new TitledBorder(new EtchedBorder(), "TICKET DE COMPRA"));
		this.add(central, BorderLayout.CENTER);
		txtDelTicket = new JTextArea("", 30, 32);
		txtDelTicket.setEditable(false);
		JScrollPane scroll = new JScrollPane(txtDelTicket);
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		central.add(scroll, BorderLayout.NORTH);
		this.pack();
		txtDelTicket.setText("*************************TICKET G-MARKET***********************"+"\n"+"\n");
		txtDelTicket.append("Fecha: " + s + "\t" +"\t" +"Hora: " + hora+ ":" + minutos + ":" + segundos+"\n" );
		//txtDelTicket.append("--------------------------------------------------------------------------------------"+"\n");
		txtDelTicket.append("\n"+"\n"+"\n");
		this.listaProductos();
		String aux = Integer.toString(t.gettotalFinal());
		txtDelTicket.append("--------------------------------------------------------------------------------------");
		txtDelTicket.append("\n"+ "\t" +"\t" +"\t" +"TOTAL = "+aux+" �"+"\n");
		txtDelTicket.append("--------------------------------------------------------------------------------------");
		//txtDelTicket.append("\n"+"EFECTIVO = "+this.importePagado+"�"+"\n");
		
		
		txtDelTicket.append("\n"+"USUARIO: "+this.aux+"\n");
		txtDelTicket.append("\n"+"CONTRASE�A: "+"****"+"\n");
		
		txtDelTicket.append("\n" +"N�MERO DE FACTURA: "+"\t"+compra.getnclienteString()+"\n");
		txtDelTicket.append("N�MERO DE VENDEDOR: "+"\t"+compra.getnVendedor()+"\n");
		txtDelTicket.append("--------------------------------------------------------------------------------------"+"\n");
		txtDelTicket.append("Guarde este ticket para posibles devoluciones en *G-MARKET*"+"\n");
		txtDelTicket.append("Presentando tu ticket en un m�x. de 30 d�as"+"\n");


	}
	
	
	
	/**
	 * 
	 * @param string
	 */
	
	public TicketDeCompra(Compra t,String cambioGenerado,String importePagado) {
		super();
		this.compra = t;
		this.cambio =cambioGenerado;
		this.importePagado = importePagado;
		this.setTitle("TICKET");
		this.setBounds(500, 100, 400, 500);
		this.setVisible(true);
		this.setResizable(false);

		Calendar calendario = new GregorianCalendar();
		int hora, minutos, segundos;
		hora = calendario.get(Calendar.HOUR_OF_DAY);
		minutos = calendario.get(Calendar.MINUTE);
		segundos = calendario.get(Calendar.SECOND);
		/**
		 * fecha
		 */
		Date now = new Date();
		DateFormat df = DateFormat.getDateInstance();
		String s = df.format(now);
		/**
		 * 
		 */
		
				
		this.setLayout(new BorderLayout(0, 0));

		central = new JPanel();
		central.setBorder(new TitledBorder(new EtchedBorder(), "TICKET DE COMPRA"));
		this.add(central, BorderLayout.CENTER);
		txtDelTicket = new JTextArea("", 30, 32);
		txtDelTicket.setEditable(false);
		JScrollPane scroll = new JScrollPane(txtDelTicket);
		scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		central.add(scroll, BorderLayout.NORTH);
		this.pack();
		txtDelTicket.setText("*************************TICKET G-MARKET***********************"+"\n"+"\n");
		txtDelTicket.append("Fecha: " + s + "\t" +"\t" +"Hora: " + hora+ ":" + minutos + ":" + segundos+"\n" );
		//txtDelTicket.append("--------------------------------------------------------------------------------------"+"\n");
		txtDelTicket.append("\n"+"\n"+"\n");
		this.listaProductos();
		String aux = Integer.toString(t.gettotalFinal());
		txtDelTicket.append("--------------------------------------------------------------------------------------");
		txtDelTicket.append("\n"+ "\t" +"\t" +"\t" +"TOTAL = "+aux+" �"+"\n");
		txtDelTicket.append("--------------------------------------------------------------------------------------");
		txtDelTicket.append("\n"+"EFECTIVO = "+this.importePagado+"�"+"\n");
		if(this.cambio == null)
		{
			txtDelTicket.append("\n"+ "\t" +"\t" +"\t" +"CAMBIO = "+0+"�"+"\n");
		}
		else
		{
		txtDelTicket.append("CAMBIO = "+this.cambio+"�"+"\n");
		}
		txtDelTicket.append("\n" +"N�MERO DE FACTURA: "+"\t"+compra.getnclienteString()+"\n");
		txtDelTicket.append("N�MERO DE VENDEDOR: "+"\t"+compra.getnVendedor()+"\n");
		txtDelTicket.append("--------------------------------------------------------------------------------------"+"\n");
		txtDelTicket.append("Guarde este ticket para posibles devoluciones en *G-MARKET*"+"\n");
		txtDelTicket.append("Presentando tu ticket en un m�x. de 30 d�as"+"\n");


	}
	
	
	/**
	 * 
	 * @param string
	 */
	private void anadirTexto (String string){
		txtDelTicket.append( string);
	}
	
	private void listaProductos(){
		String mensaje = "";
		ArrayList<String[]> compra = this.compra.listaCompra();
		for (int i = 0; i < compra.size(); i++){
			mensaje = compra.get(i)[0] + "\t" + compra.get(i)[1]+ "\t" +"\t"+compra.get(i)[2] +"�"+ "\n";
			this.anadirTexto( mensaje );
		}
	}


}
